<template>
  <div class="com-container" @dblclick="revertMap">
    <div class="com-chart" ref="map_ref"></div>
  </div>
</template>
<script>
// import { getProvinceMapInfo } from '@/utils/map_utils.js'
import { mapState } from 'vuex'
// import axios from 'axios'
import chinaJson from '@/assets/json/china.json'
export default {
  data() {
    return {
      chartInstance: null,
      // allData: null // 服务器返回的数据
      allData: [
        {
          name: "黄金用户",
          children: [
            {
              name: "武汉",
              value: [114.31, 30.52]
            },
            {
              name: "丹东",
              value: [124.37, 40.13]
            },
            {
              name: "张家口",
              value: [114.87, 40.82]
            },
            {
              name: "深圳",
              value: [114.07, 22.62]
            }
          ]
        },
        {
          name: "白金用户",
          children: [
            {
              name: "金华",
              value: [119.64, 29.12]
            },
            {
              name: "西安",
              value: [108.95, 34.27]
            }
          ]
        },
        {
          name: "砖石用户",
          children: [
            {
              name: "成都",
              value: [104.06, 30.67]
            }
          ]
        }
      ],
      mapData: {}, //获取的省份的地图矢量数据
      chinaJson: chinaJson,
    };
  },
  mounted() {
    this.initChart();
    this.getData();
    window.addEventListener("resize", this.screenAdapter);
    this.screenAdapter();
    // console.log(this.chinaJson);
  },
  destroyed() {
    window.removeEventListener("resize", this.screenAdapter);
  },
  methods: {
    async initChart() {
      this.chartInstance = this.$echarts.init(this.$refs.map_ref, this.theme);
      // 访问的是本地文件,所以不能直接使用this.$http
    //   const res = await axios.get(
    //     "http://120.53.120.229:9997"
    //   );
    //   console.log(res);
      this.$echarts.registerMap("china", this.chinaJson);
      const initOption = {
        title: {
          text: "▍商家分布",
          left: 20,
          top: 12
        },
        geo: {
          type: "map",
          map: "china",
          roam: true,//实现地图拖拽及鼠标滚轮滑动缩放
          top: '5%',
          bottom: '5%',
          itemStyle: {
            areaColor: 'rgba(46, 114, 204, 0.8)',
            borderColor: '#333'
          },
          label: {
            show: true,
            color: 'white',
          },
        },
        legend: {
          left: '5%',
          bottom: '5%',
          orient: 'vertical'
        }
      };
      this.chartInstance.setOption(initOption);
    //   this.chartInstance.on('click', async arg => {
    //     // console.log(arg); //arg.name 点击得到的省份是中文
    //     const provinceInfo = getProvinceMapInfo(arg.name);
    //     // 判断当前所点击的这个省份的地图矢量数据在mapData中是否存在
    //     if(!this.mapData[provinceInfo.key]){
    //       // console.log(provinceInfo);
    //       const res = await axios.get('http://120.53.120.229:9997' + provinceInfo.path)
    //       this.mapData[provinceInfo.key] = res.data
    //       this.$echarts.registerMap(provinceInfo.key, res.data)
    //     }
    //     const changeOption = {
    //       geo: {
    //         map: provinceInfo.key
    //       }
    //     }
    //     this.chartInstance.setOption(changeOption)
    //   })
    },
    async getData() {
      // const { data: res } = await this.$http.get('map');
      // console.log(res);
      // this.allData = res;
      this.updateChart();
    },
    updateChart() {
      const legendArr = this.allData.map(item => {
        return item.name
      })
      const seriesArr = this.allData.map(item => {
        // return的这个对象就代表的是一个类别下的所有散点数据
        // 如果想在地图中现实散点的数据,所以我们需要给散点的图表增加一个配置,coordinateSystem:geo
        return {
          type: 'effectScatter',
          rippleEffect: {
            scale: 6,
            brushType: 'stroke'
          },
          name: item.name,
          data: item.children,
          coordinateSystem: 'geo'
        }
      })
      const dataOption = {
        legend: {
          data: legendArr
        },
        series: seriesArr
      };
      this.chartInstance.setOption(dataOption);
    },
    screenAdapter() {
      const titleFontSize = (this.$refs.map_ref.offsetWidth / 100) * 3.6;
      const adapterOption = {
        title: {
          textStyle: {
            fontSize: titleFontSize 
          }
        },
        geo: {
            label: {
            textStyle: {
            fontSize: titleFontSize / 2
          }
        }
        },
        legend: {
          itemWidth: titleFontSize / 1.7,
          itemHeight: titleFontSize / 1.7,
          itemGap: titleFontSize / 2,
          textStyle: {
            fontSize: titleFontSize / 1.7
          }
        }
      };
      this.chartInstance.setOption(adapterOption);
      this.chartInstance.resize();
    },
    // 双击回到中国地图
    revertMap(){
      const revertOption = {
        geo: {
          map: 'china'
        }
      }
      this.chartInstance.setOption(revertOption)
    }
  },
  computed: {
    ...mapState(['theme'])
  },
  watch: {
    theme(){
      this.chartInstance.dispose();//销毁当前的图表
      this.initChart(); // 重新以最新的主题名称初始化图表对象
      this.screenAdapter(); // 完成屏幕的适配
      this.updateChart(); // 更新图表的展示
    }
  }
};
</script>
<style scoped>
</style>
